package com.lele.service.impl;
import java.time.LocalDateTime;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lele.constant.MailContant;
import com.lele.enums.MailEnums;
import com.lele.mapper.EmployeeEcMapper;
import com.lele.mapper.EmployeeMapper;
import com.lele.mapper.MailLogMapper;
import com.lele.pojo.Employee;
import com.lele.pojo.EmployeeEc;
import com.lele.pojo.MailLog;
import com.lele.pojo.vo.EmployeeVo;
import com.lele.service.IEmployeeService;
import com.lele.utils.RespPage;
import com.mobaijun.common.annotation.poi.Excel;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lele
 * @since 2022-11-26
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private MailLogMapper mailLogMapper;


/*    @Override
    public RespPage getEmployee(Integer pageNum, Integer pageSize, Employee employee, LocalDate[] beginDateScope) {
        //开启分页
        Page<Employee> page = new Page<>(pageNum,pageSize);
        IPage<EmployeeVo> employeeIPage=employeeEcMapper.getEmployee(page,employee,beginDateScope);
        RespPage respPage = new RespPage(employeeIPage.getTotal(), employeeIPage.getRecords());
        return respPage;
    }*/

    @Override
    public RespPage getEmployee(Integer pageNum, Integer pageSize, Employee employee, LocalDate[] beginDateScope) {
        return null;
    }

    @Override
    public List<Employee> exportEmployee() {
        //获取到需要转为excel的数据，并且在实体类上加上相应的注解
        List<Employee> employees = employeeMapper.selectList(null);
        return employees;
    }

    @Override
    public Employee sendMail(Integer id) {
        Employee employee = this.baseMapper.selectById(id);
        //发送信息
        MailLog mailLog = new MailLog();
        String s = UUID.randomUUID().toString().replace("-","");
        mailLog.setMsgId(s);
        mailLog.setEid(id);
        mailLog.setStatus(MailEnums.MESSAGE_DELIVERYING.getCode());
        mailLog.setRouteKey(MailContant.MAIL_ROUTER);
        mailLog.setExchange(MailContant.MAIL_EXCHANGE);
        mailLog.setCount(0);
        //一分钟后重试
        mailLog.setTryTime(LocalDateTime.now().plusSeconds(MailContant.AGAIN_TIME));
        mailLog.setCreateTime(LocalDateTime.now());
        mailLog.setUpdateTime(LocalDateTime.now());
        mailLogMapper.insert(mailLog);
        rabbitTemplate.convertAndSend("lele",MailContant.MAIL_ROUTER,employee,new CorrelationData(s));
        return employee;
    }
}
